package com.lanou3g.code0418.dirty;

import java.util.concurrent.locks.ReentrantReadWriteLock;

/**
 * If there are no bugs, it was created by Chen FengYao on 2018/4/17;
 * Otherwise, I don't know who created it either
 */

public class Utils {
    private ReentrantReadWriteLock lock
            = new ReentrantReadWriteLock();

    public void updatePerson(Person person
            , String name, int age) {

        lock.writeLock().lock();

        person.setName(name);
        // 让线程沉睡1s,为了让脏读效果更容易出现
        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        person.setAge(age);

        lock.writeLock().unlock();
    }


    public void showPerson(Person person) {
        lock.readLock().lock();
        System.out.println("姓名:" + person.getName());
        System.out.println("年龄:" + person.getAge());
        lock.readLock().unlock();
    }
}
